<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="equipmentStatus">
        <el-select
          v-model="queryParams.equipmentStatus"
          placeholder="请选择设备状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sysk_equipment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="useStatus">
        <el-select
          v-model="queryParams.useStatus"
          placeholder="请选择使用状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sysk_equipment_usestatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:equipmentinfo:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['equipment:equipmentinfo:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="logicDelete"
          v-hasPermi="['equipment:equipmentinfo:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:equipmentinfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="equipmentinfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="设备型号" align="center" prop="model" />
      <el-table-column label="库房名称" align="center" prop="storeName" />
      <el-table-column label="设备状态" align="center" prop="equipmentStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sysk_equipment_status"
            :value="scope.row.equipmentStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="useStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sysk_equipment_usestatus"
            :value="scope.row.useStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="selectEquipment(scope.row)"
            >查看</el-button
          >

          <el-button type="text" @click="editEquipment(scope.row)">编辑</el-button>

          <!-- <el-button type="text" @click="applyTest(scope.row)">申请试验</el-button> -->
          <el-button
            size="mini"
            type="text"
            v-if="
              scope.row.equipmentStatus == 0 || scope.row.equipmentStatus == 2
            "
            @click="applyTest(scope.row)"
          >
            申请试验</el-button
          >

          <el-button
            size="mini"
            type="text"
            @click="confirm1(scope.row)"
            v-if="scope.row.equipmentStatus == 1"
          >
            试验结果
          </el-button>

          <!-- 点击实验结果后弹出对话框选择成功还是失败 -->
          <el-dialog title="实验结果" :visible.sync="dialogFormVisible">
            <el-form :model="form">
              <el-form-item label="成功还是失败?" :label-width="formLabelWidth">
                <el-select v-model="form1.testResult" placeholder="请选择实验结果">
                  <el-option label="成功" value=1></el-option>
                  <el-option label="失败" value=0></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <!-- //上传附件 -->
            <el-form label="附件" prop="attach">
              <file-upload v-model="form1.testAttach" />
            </el-form>

            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="resultTest(scope.row)"
                >确 定</el-button
              >
            </div>
          </el-dialog>

          <el-button
            type="text"
            @click="outStore(scope.row)"
            :disabled="
              scope.row.equipmentStatus == 1 && scope.row.useStatus == 0
                ? false
                : true
            "
            >出库</el-button
          >

          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:equipmentinfo:remove']">删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="logicDelete(scope.row)"
            v-hasPermi="['equipment:equipmentinfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参试设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="disable">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-select v-model="form.deptId" placeholder="请选择部门">
            <el-option
              v-for="dept in deptList"
              :key="dept.deptId"
              :label="dept.deptName"
              :value="dept.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="存放位置" prop="storeId">
          <el-select v-model="form.storeId" placeholder="请选择库房">
            <el-option
              v-for="storeHouse in houseList"
              :key="storeHouse.id"
              :label="storeHouse.name"
              :value="storeHouse.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件" prop="attach">
          <file-upload v-model="form.attach" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  resultOfTest,
  applyForTest,
  outOfStore,
  listEquipmentinfo,
  getEquipmentinfo,
  delEquipmentinfo,
  logicDelEquipmentinfo,
  addEquipmentinfo,
  updateEquipmentinfo,
} from "@/api/equipment/equipmentinfo";
import { listDept } from "@/api/system/dept";
import { listHouse, listUnFullHouse } from "@/api/store/house";
import { listEquipmentlog } from "../../../api/equipment/equipmentlog";
import { Upload } from "element-ui";
// 新增使用记录的方法
import { addEquipmentuse } from "@/api/equipment/equipmentuse";

export default {
  name: "Equipmentinfo",
  dicts: ["sysk_equipment_usestatus", "sysk_equipment_status"],
  data() {
    return {
      disable: false,
      // 点击实验结果所弹出的表单.
      dialogFormVisible: false,
      form1: {
        id:"",
        testAttach: "",
        testResult:""
      },
      formLabelWidth: "120px",

      fileList: [],

      houseList: [],
      deptList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参试设备表格数据
      equipmentinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        model: null,
        equipmentStatus: null,
        useStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        storeId: [
          { required: true, message: "请选择存放库房", trigger: "change" },
        ],
        remark: [
          {
            required: true,
            message: "请添加一条备注说明用途",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        capacity: [
          {
            required: true,
            message: "请输入一个整数",
            trigger: "blur",
          },
          {
            pattern: /^[+-]?\d+$/,
            message: "输入的值必须是一个整数",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    this.getHouseList();
  },
  methods: {
    //实验结果确定按钮
    confirm1(row){
      this.dialogFormVisible=true;
      this.form1.id=row.id;
    },
    //查看按钮操作
    selectEquipment(row) {
      const id = row.id || this.ids;
      getEquipmentinfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看参试设备";
      });
      this.disable=true;
    },
    //编辑按钮操作
    editEquipment(row){
      const id = row.id || this.ids;
      getEquipmentinfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参试设备";
      });
    },
    //文件上传
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },

    //确定结果按钮
    resultTest() {
      this.form1.testResult=Number(this.form1.testResult)
      this.dialogFormVisible = !this.dialogFormVisible;
      resultOfTest(this.form1).then((response) => {
        this.getList();
      });
    },

    //申请试验按钮
    applyTest(row) {
      const id = row.id || this.ids;
      applyForTest(id).then((response) => {
        this.getList();
      });
    },

    //出库按钮
    outStore(row) {
      const id = row.id || this.ids;
      outOfStore(id).then((response) => {
        // this.addEquipmentuse();
        this.getList();
      });
    },

    // //出库按钮是否被禁用
    // isButtonEnable(){
    //   this.equipmentStatus==1 && this.useStatus==0;
    // },

    //查询所有未满的库房
    getHouseList() {
      listUnFullHouse().then((result) => {
        this.houseList = result.rows;
      });
    },
    //查询所有部门
    getDeptList() {
      listDept().then((result) => {
        this.deptList = result.data;
      });
    },
    /** 查询参试设备列表 */
    getList() {
      this.loading = true;
      listEquipmentinfo(this.queryParams).then((response) => {
        this.equipmentinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
        //更新库房日志
        this.listEquipmentlog;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        deptId: null,
        model: null,
        storeId: null,
        attach: null,
        remark: null,
        equipmentStatus: null,
        useStatus: null,
        createUser: null,
        createTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参试设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEquipmentinfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参试设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipmentinfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              // this.listEquipmentlog();
            });
          } else {
            addEquipmentinfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              // this.listEquipmentlog();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除参试设备编号为"' + ids + '"的数据项？').then(function () {
    //     return delEquipmentinfo(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => { });
    // },
    //  逻辑删除按钮
    logicDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除参试设备编号为"' + ids + '"的数据项？')
        .then(function () {
          return logicDelEquipmentinfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/equipmentinfo/export",
        {
          ...this.queryParams,
        },
        `equipmentinfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
